/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    if (s && s.length <= 1) return s

    let length = 0, res = ''
    for (let i = 0; i < s.length; i++) {
        // 判断中心是一位还是两位
        length = Math.max(getExpendStr(s, i, i), getExpendStr(s, i, i + 1))
        res = res.length > length ? res : s.substr(i - Math.floor((length - 1) / 2), length)
    }
    return res
};

function getExpendStr(s, l, r) {
    while (l >= 0 && r < s.length && s.charAt(l) === s.charAt(r)) {
        l--
        r++
    }
    return r - l - 1
}